home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / comms / netsoftware / nethandler.lha / NetHandler / handler / io.c < prev    next >
C/C++ Source or Header  |  1989-09-16  |  8KB  |  236 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* |_o_o|\\ Copyright (c) 1987, 1988 The Software Distillery.  All Rights  */
  3. /* |. o.| || Reserved.  This program may not be distributed without the    */
  4. /* | .  | || permission of the authors:                            BBS:    */
  5. /* | o  | ||   John Toebes     Doug Walker    Dave Baker                   */
  6. /* |  . |//                                                                */
  7. /* ======                                                                  */
  8. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9. /* File Access:            */
  10. /* ActRead ActWrite ActSeek ActWaitForChar    */
  11. /* ActFindwrite ActFindin ActFindout ActEnd   */
  12.  
  13. #include "handler.h"
  14.  
  15. /*-------------------------------------------------------------------------*/
  16. /*                                                                         */
  17. /*                 ActRead( global, pkt )                                  */
  18. /*                                                                         */
  19. /*-------------------------------------------------------------------------*/
  20.  
  21. void ActRead(global,pkt)
  22. GLOBAL global;
  23. struct DosPacket *pkt;              /* a pointer to the dos packet sent    */
  24. /* Arg1: APTR EFileHandle */
  25. /* Arg2: APTR Buffer      */
  26. /* Arg3: Length           */
  27. {
  28.    NETPTR nfile;
  29.    long toread, total, amtread;
  30.    char *data;
  31.  
  32.    BUG(("ActRead\n"));
  33.  
  34.    nfile = (NETPTR)pkt->dp_Arg1;
  35.  
  36.    global->RP.Type = pkt->dp_Type;
  37.    global->RP.Arg1 = (LONG)nfile->RPtr;
  38.    global->RP.Arg3 = toread = pkt->dp_Arg3;
  39.  
  40.    for(data=(char *)pkt->dp_Arg2, total=0L; toread>0; toread-=amtread)
  41.    {
  42.       global->RP.DLen = 0;
  43.       if(RemotePacket(global, nfile))
  44.          return;
  45.  
  46.       if ((amtread=global->RP.Arg1) == 0)
  47.          break;
  48.  
  49.       if(amtread<0)
  50.       {
  51.          BUG(("Read error: codes %lx,m, %lx\n",
  52.             global->RP.Arg1,global->RP.Arg2));
  53.          total = global->RP.Arg1;
  54.          goto AFTER;
  55.       }
  56.  
  57.       MQ(global->RP.Data, data, amtread);
  58.       total += amtread;
  59.       data += amtread;
  60.    }
  61. AFTER:
  62.    pkt->dp_Res1 = total;
  63.    pkt->dp_Res2 = global->RP.Arg2;
  64. }
  65.  
  66. /*-------------------------------------------------------------------------*/
  67. /*                                                                         */
  68. /*                      ActWrite( global, pkt )                            */
  69. /*                                                                         */
  70. /*-------------------------------------------------------------------------*/
  71.  
  72. /* This should probably be changed to look for a confirmation after */
  73. /* each write in order to notice errors faster - djw                */
  74.  
  75. void ActWrite(global,pkt)
  76. GLOBAL global;
  77. struct DosPacket *pkt;              /* a pointer to the dos packet sent       */
  78. /* Arg1: APTR EFileHandle */
  79. /* Arg2: APTR Buffer */
  80. /* Arg3: Length */
  81. {
  82.    NETPTR nfile;
  83.    long towrite, total, amtwrite;
  84.    char *data;
  85.  
  86.    BUG(("ActWrite\n"));
  87.  
  88.    nfile = (NETPTR)pkt->dp_Arg1;
  89.  
  90.    global->RP.Type = pkt->dp_Type;
  91.    global->RP.Arg1 = (LONG)nfile->RPtr;
  92.    towrite = pkt->dp_Arg3;
  93.  
  94.    for(data = (char *)pkt->dp_Arg2, total = 0L; 
  95.        towrite > 0; 
  96.        towrite -= amtwrite, data += amtwrite)
  97.    {
  98.       amtwrite = min(towrite, NETBUFSIZE);
  99.       MQ(data, global->RP.Data, amtwrite);
  100.       global->RP.DLen = global->RP.Arg3 = amtwrite;
  101.       global->RP.Arg4 = (amtwrite == towrite ? 0 : 1);
  102.  
  103.       if(RemotePacket(global, nfile) || pkt->dp_Res1 == -1) break;
  104.  
  105.       total += pkt->dp_Res1;
  106.    }
  107.  
  108.    if(pkt->dp_Res1 >= 0) pkt->dp_Res1 = total;
  109.  
  110.    BUG(("ActWrite: Ending, %d bytes written\n", total));
  111. }
  112.  
  113. /*---------------------------------------------------------------------------*/
  114. /*                                                                           */
  115. /*                       ActSeek( global, pkt )                              */
  116. /*                                                                           */
  117. /*---------------------------------------------------------------------------*/
  118.  
  119. void ActSeek(global,pkt)
  120. GLOBAL global;
  121. struct DosPacket *pkt;              /* a pointer to the dos packet sent      */
  122. /* Arg1: APTR EFileHandle */
  123. /* Arg2: Position */
  124. /* Arg3: Mode */
  125. {
  126.    NETPTR nfile;
  127.  
  128.    BUG(("ActSeek\n"));
  129.  
  130.    nfile = (NETPTR)pkt->dp_Arg1;
  131.  
  132.    global->RP.Type = pkt->dp_Type;
  133.    global->RP.Arg1 = (LONG)nfile->RPtr;
  134.    global->RP.Arg2 = pkt->dp_Arg2;
  135.    global->RP.Arg3 = pkt->dp_Arg3;
  136.    global->RP.DLen = 0;
  137.  
  138.    RemotePacket(global, nfile);
  139. }
  140.  
  141. /*-------------------------------------------------------------------------*/
  142. /*                                                                         */
  143. /*                    ActFindwrite( global, pkt )                          */
  144. /*                                                                         */
  145. /*-------------------------------------------------------------------------*/
  146.  
  147. void ActFindwrite(global,pkt)
  148. GLOBAL global;
  149. struct DosPacket *pkt;              /* a pointer to the dos packet sent    */
  150. /* ARG1: FileHandle to fill in */
  151. /* ARG2: Lock for file relative to */
  152. /* Arg3: Name of file */
  153. {
  154.    struct FileHandle *fh;
  155.    NETPTR nlock, nfile;
  156.    struct FileLock *flock;
  157.  
  158.    BUG(("ActFindwrite\n"));
  159.    /* Code 1004 - 
  160.       If file does not exist, open should fail.
  161.       If file does exist, open with an exclusive lock */
  162.  
  163.    fh = (struct FileHandle *)pkt->dp_Arg1;
  164.  
  165.    if((!(flock=(struct FileLock *)pkt->dp_Arg2) || 
  166.        !(nlock=(NETPTR)flock->fl_Key)->RDevice))
  167.    {
  168.       if(ParseName(global, (char *)pkt->dp_Arg3, &nlock, global->RP.Data) ||
  169.            !nlock)
  170.       {
  171.          /* Can't open files in NET: itself */
  172.          pkt->dp_Res1 = NULL;
  173.          pkt->dp_Res2 = (nlock ? ERROR_OBJECT_NOT_FOUND 
  174.                                : ERROR_OBJECT_WRONG_TYPE);
  175.          return;
  176.       }
  177.    }
  178.    else
  179.       MBSTR(pkt->dp_Arg3, global->RP.Data);
  180.  
  181.    global->RP.Type = pkt->dp_Type;
  182.    global->RP.Arg2 = (LONG)nlock->RPtr;
  183.    global->RP.DLen = BSTRLEN(global->RP.Data)+1;
  184.    if(global->RP.DLen == 1) global->RP.DLen = 0;
  185.  
  186.    if(!RemotePacket(global, nlock) && pkt->dp_Res1 != DOS_FALSE)
  187.    {
  188.       BUG(("ActFind: Valid local filehandle = %lx\n", fh));
  189.       if(!(nfile=(NETPTR)DosAllocMem(global, 
  190.                      (long)sizeof(struct NetPtr))))
  191.       {
  192.          BUG(("******NO MEMORY FOR LOCAL LOCK"));
  193.          pkt->dp_Res1 = DOS_FALSE;
  194.          pkt->dp_Res2 = ERROR_NO_FREE_STORE;
  195.          return;
  196.       }
  197.  
  198.       nfile->NetNode = nlock->NetNode;
  199.       nfile->RDevice = nlock->RDevice;
  200.       /* Remote filehandle passed back in RP.Arg3 */
  201.       nfile->RPtr    = (RPTR)global->RP.Arg3;
  202.  
  203.       fh->fh_Arg1 = (LONG)nfile;
  204.    }
  205. #if DEBUG
  206.    else
  207.       BUG(("ActFind: Remote file open failed, codes %d %d\n",
  208.          pkt->dp_Res1, pkt->dp_Res2));
  209. #endif
  210. }
  211.  
  212. /*-------------------------------------------------------------------------*/
  213. /*                                                                         */
  214. /*                       ActEnd( global, pkt )                             */
  215. /*                                                                         */
  216. /*-------------------------------------------------------------------------*/
  217.  
  218. void ActEnd( global, pkt )
  219. GLOBAL global;
  220. struct DosPacket *pkt;              /* a pointer to the dos packet sent    */
  221. {
  222.    NETPTR nfile;
  223.  
  224.    BUG(("ActEnd\n"));
  225.  
  226.    nfile = (NETPTR)pkt->dp_Arg1;
  227.  
  228.    global->RP.Type = pkt->dp_Type;
  229.    global->RP.Arg1 = (LONG)nfile->RPtr;
  230.    global->RP.DLen = 0;
  231.  
  232.    RemotePacket(global, nfile);
  233.  
  234.    DosFreeMem((char *)nfile);
  235. }
  236.